<template>
  <view class="container">
      <view class='login_content'>
          <view class="user_photo">
              <open-data type="userAvatarUrl"></open-data>
          </view>
          <view class='user_nickname' style="color: #333333"><open-data type="userNickName"></open-data></view>
          <view class="login-btn" @click="bindGetUserInfo">
              <image class="login-icon" mode="aspectFit" src="../../static/img/wx.png"></image>
              <text>微信授权登录</text>
          </view>
          <view class="desc">
              <text>人工智能党建助手手机端介绍：</text>
              <text>1、参与内部会议</text>
              <text>2、手机同步PPT内容</text>
              <text>3、参与会议讨论</text>
          </view>
      </view>
  </view>
</template>
<script>
import { postCode } from '../../api/index.js'
import { mapActions } from 'vuex'

export default {
  data () {
    return {
      key:""
    }
  },
  onLoad(query){
    const scene = decodeURIComponent(query.scene)

    var key  = "";
    var type = "";
    if(!scene){
      key = query.key
      type = query.type
    }else {
      key  = this.getQueryVariable('key',scene)
      type = this.getQueryVariable('type',scene)
    }

    if(key){
      this.key = key
    }

    console.log("keykeykey",this.key)
  },
  methods: {
    ...mapActions(['_reqUserInfo']),
    bindGetUserInfo(e){
      uni.showLoading()
      var This = this;
          wx.getUserProfile({
            lang : 'zh_CN',
            desc: '获取您的昵称和头像信息',
            success : res2 => {
              console.log(res2)
              This.$store.dispatch('_setWXUserInfo',res2); //将用户信息保存的VUEX
              wx.login({
                success: res => {
                  const code = res.code;
                  console.log(code)
                  let parmas = {
                    raw_data:res2.rawData,
                    signature:res2.signature,
                    code:code
                  };
                  //发送code到服务器获取数据
                  postCode(parmas).then(respones => {
                    console.log(respones)
                    if(respones.data.level=="success"){
                      uni.setStorageSync('userToken',respones.data.data);
                      // uni.navigateBack({
                      //   delta:1
                      // })
                      uni.redirectTo({
                        url:"/pages/index/index"
                      })
                      wx.showToast({
                        title: '登陆成功',
                        icon: 'success',
                        duration: 2000
                      })
                    }else{
                      uni.hideLoading()
                      uni.showToast(respones.data.message,'none',1000)
                    }
                  })
                },
              });
            }
          })
      // let code = this.$store.state.userCode;

    },

    getQueryVariable(variable,scene){
      var vars = scene.split("&");
      for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
      }
      return(false);
    },
    async checkDeviceLogin(){
      var _this = this
      let params = {
        auth_key:this.key,
        user_token: uni.getStorageSync('userToken'),
      }
      let res = await _this.$api.smallAppLoginCheck(params);
      // if(res.data.level == "success"){
      // uni.showToast({
      //   title: res.data.message,
      //   icon: 'none',
      //   duration: 1000
      // })
      // }
    },
  }
}
</script>
<style lang="scss" scoped>
    .container {
        width: 100vw;
        height: 100vh;
        background: url("https://rzkeji.oss-cn-shenzhen.aliyuncs.com/web/dang/login_background.png") no-repeat;
        background-size: 100% 100%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .login_content{
            width: 70%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            position: relative;
            top: -30rpx;
            .desc{
                color: #333333;
                margin-top: 30px;
                display: flex;
                flex-direction: column;

                text{
                    padding-left: 10px;
                    padding-top: 5px;
                    font-size: 0.8rem;
                }
            }
            .login_btn{
                width: 100%;
            }
            .user_photo{
                width: 200rpx;
                height: 200rpx;
                border-radius: 50%;
                overflow: hidden;
            }
            .user_nickname{
                text-align: center;
            }
            .login-btn {
                width: 95%;
                height: 90rpx;
                background:linear-gradient(to right, #ff302f,#dc1411);
                color: #ffffff;
                display: flex;
                align-items: center;
                justify-content: center;
                border-radius: 50rpx;
                margin-left: auto;
                margin-right: auto;
                margin-top: 60rpx;
                .login-icon {
                    width: 46rpx;
                    height: 36rpx;
                    margin-right: 30rpx;
                }
            }
        }
    }
</style>
